from fastapi import FastAPI, UploadFile, Form, HTTPException
from .file.main import save_file
from ocr.cert.card import initCard
from ocr.cert.config import cardFn
import paddleclas

upload = FastAPI()

# 识别身份证接口
@upload.post("/card")
async def upload_cert(type: int = Form(), file: UploadFile = Form()):
    filename = await save_file(file)
    if filename is not None:
        opts = {}
        if cardFn[type] is not None:
            opts = getattr(initCard, cardFn[type])(filename)
            return opts
        else:
            return opts
    else:
        raise HTTPException(status_code=500, detail="文件上传失败")

# 识别身份证
@upload.post("/sfz")
async def upload_sfz(file: UploadFile):
    filename = await save_file(file)
    if filename is not None:
        opts = getattr(initCard, 'get_sfz')(filename)
        return opts
    else:
        return None
# 识别营业执照接口
@upload.post("/yyzz")
async def upload_yyzz(file: UploadFile):
    filename = await save_file(file)
    if filename is not None:
        opts = getattr(initCard, 'get_yyzz')(filename)
        return opts
    else:
        return None

# 识别户口簿
@upload.post("/hkb")
async def upload_hkb(file: UploadFile):
    filename = await save_file(file)
    if filename is not None:
        # opts = getattr(initCard, 'get_hkb')(filename)
        return filename
    else:
        return None
# 识别港澳台居民居住证
@upload.post("/jzz")
async def upload_jzz(file: UploadFile):
    filename = await save_file(file)
    if filename is not None:
        # opts = getattr(initCard, 'get_jzz')(filename)
        return filename
    else:
        return None
# 识别外国人永久居留身份证
@upload.post("/jlsfz")
async def upload_jlsfz(file: UploadFile):
    filename = await save_file(file)
    if filename is not None:
        # opts = getattr(initCard, 'get_jlsfz')(filename)
        return filename
    else:
        return None
# 识别护照
@upload.post("/hz")
async def upload_hz(file: UploadFile):
    filename = await save_file(file)
    if filename is not None:
        # opts = getattr(initCard, 'get_hz')(filename)
        return filename
    else:
        return None
@upload.post("/file")
def upload_file(file: UploadFile):
    return 1222222222222222222222222

@upload.post("files")
def upload_files(files: list[UploadFile]):
    return ""

@upload.get("/file", )
async def upload_jpg(id: int=1):
    filename = f'D:/cache/resource/image/jpeg/20240901/{id}.jpg'
    model = paddleclas.PaddleClas(model_name='text_image_orientation')
    result = model.predict(input_data=filename)
    osd = next(result)

    print(osd)
    return "ssssss"